<include file="public/min-header"/>
<div class="wrapper">
    <include file="public/breadcrumb"/>
	<section class="content">
       <div class="row">
       		<div class="col-xs-12">
	       	  <div class="box">
	           	<div class="box-header">
	               <nav class="navbar navbar-default">	     
				        <div class="collapse navbar-collapse">
				          <form class="navbar-form form-inline">
				          	<div class="input-group"><p>数据库中共有{$tableNum}张表，共计{$total}</p></div>
				            <div class="form-group pull-right">
					            <a href="javascript:void(0)" onclick="gobackup(this)" class="btn btn-primary pull-right"><i class="fa fa-eject"></i>备份</a>
				            </div>
				          </form>
				      	</div>
	    			</nav>               
	            </div>	    
	             <!-- /.box-header -->
	             <div class="box-body">	             
	           		<div class="row">
	            	<div class="col-sm-12">
	            	<form  method="post" id="export-form" action="">
		              <table class="table table-bordered table-striped">
		                 <thead>
		                   <tr>
		                       <th class="text-center" style="width: 2px;"><input type="checkbox" onclick="javascript:$('input[name*=backs]').prop('checked',this.checked);"></th>
			                   <th class="sorting" tabindex="0">数据库表</th>
			                   <th class="sorting" tabindex="0">记录条数</th>
			                   <th class="sorting" tabindex="0">占用空间</th>
			                   <th class="sorting" tabindex="0">编码</th>
			                   <th class="sorting" tabindex="0">创建时间</th>
			                   <th class="sorting" tabindex="0">备份状态</th>
			                   <th class="sorting" tabindex="0">操作</th>
		                   </tr>
		                 </thead>
						<tbody>
						  <foreach name="list" item="vo" key="k" >
						  	<tr>
 							 <td><input type="checkbox" name="backs[]" value="{$vo.Name}"></td>
		                     <td>{$vo.Name}</td>
		                     <td>{$vo.Rows}</td>
		                     <td>{$vo.Data_length|format_bytes}</td>
		                     <td>{$vo.Collation}</td>
		                     <td>{$vo.Create_time}</td>
								<td align="center" class="">
									<div style="text-align: center; width: 200px;" class="info">未备份</div>
								</td>
		                     <td>
		                      <a class="btn btn-success" href="{:U('Tools/optimize',array('tablename'=>$vo['Name']))}">优化</a>
		                      <a class="btn btn-info" href="{:U('Tools/repair',array('tablename'=>$vo['Name']))}" data-url="">修复</a>
							</td>
		                   </tr>
		                  </foreach>
		                   </tbody>
		                 <tfoot>
		                 </tfoot>
		               </table>
		           </form>
	               </div>
	           </div>
	          </div>
	        </div>
       	</div>
       </div>
   </section>
</div>
<script type="text/javascript">
	var tables;
function gobackup(obj){
	var a = [];
	$('input[name*=backs]').each(function(i,o){
		if($(o).is(':checked')){
			a.push($(o).val());
		}
	});
	if(a.length==0){
		layer.alert('请选择要备份的数据表', {icon: 2});  //alert('请选择要备份的数据表');
		return;
	}else{
		$(obj).addClass('disabled');
		$(obj).html('备份进行中...');
		$.ajax({
			type :'post',
			url : "{:U('Tools/export')}",
			data : {tables:a},
			success : function(data){
				if(data.status==1){
                    tables = data.tables;
                    $(obj).html(data.info + "开始备份，请不要关闭本页面！");
                    backup(data.tab,obj);
                    window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
				}else{
					layer.alert(data.info, {icon: 2});
				}
			}
		})
	}
}
function backup(tab, obj){
    showmsg(tab.id, "开始备份...(0%)");
    $.get('{:U("Tools/export")}', tab, function(data){
        if(data.status){
            showmsg(tab.id, data.info);
            if(!data.tab){
                $(obj).removeClass("disabled");
                $(obj).html("备份完成，点击重新备份");
                window.onbeforeunload = function(){ return null }
                return;
            }
            backup(data.tab, obj);
        } else {
            $(obj).removeClass("disabled");
            $(obj).html("立即备份");
        }
    }, "json");
}
function showmsg(id, msg){
    $('#export-form').find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
}
</script>
</body>
</html>